<template>
  <el-form label-width="120px" ref="formRef" :model="form" :rules="formRules">
    <el-row>
      <el-col :span="12">
        <el-form-item label="角色标识" prop="code">
          <el-input :disabled="form.id && (form.code === 'TEN_SUPER_ROLE' || form.code === 'SYS_SUPER_ROLE')" clearable
                    placeholder="请输入角色标识"
                    v-model="form.code"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="角色名称" prop="name">
          <el-input clearable placeholder="请输入角色名称" v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row v-if="form.code !== 'TEN_SUPER_ROLE' && form.code !== 'SYS_SUPER_ROLE'">
      <el-col :span="24">
        <el-form-item label="菜单权限" prop="menuIdList">
          <el-tree-select ref="menuTreeSelect" size="large" clearable multiple show-checkbox check-strictly
                          highlight-current collapse-tags collapse-tags-tooltip check-on-click-node node-key="id"
                          style="width: 100%" :render-after-expand="false" :data="menuList" :props="categoryProps"
                          v-model="form.menuIdList"
                          :placeholder="(form.code === 'TEN_SUPER_ROLE' || form.code === 'SYS_SUPER_ROLE') ? '全部菜单权限' : '请选择菜单'"
                          :disabled="form.id && (form.code === 'TEN_SUPER_ROLE' || form.code === 'SYS_SUPER_ROLE')"
          ></el-tree-select>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="是否启用" prop="enabled">
          <el-radio-group v-model="form.enabled"
                          :disabled="form.id && (form.code === 'TEN_SUPER_ROLE' || form.code === 'SYS_SUPER_ROLE')">
            <el-radio-button :value="true" border>启用</el-radio-button>
            <el-radio-button :value="false" border>停用</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="1" controls-position="right" size="large"
                           placeholder="请输入排序"></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" clearable placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup lang="js">
import {onMounted, reactive, ref} from "vue";
import tree from "@/utils/tree";
import menuApi from "@/api/system/menu";
import deptApi from "@/api/system/dept";

const props = defineProps({
  form: {
    type: Object,
    default: {}
  }
})
const formRef = ref();
const categoryProps = {
  label: (data) => data.name + "-" + data.code,
  children: 'children',
}
const deptList = ref([]);
const menuList = ref([]);
const formRules = reactive({
  code: [{required: true, message: "角色标识为必填项", trigger: "blur"}],
  name: [{required: true, message: "角色名称为必填项", trigger: "blur"}],
  menuIdList: [{required: true, message: "菜单权限列表为必填项", trigger: "blur"}],
  enabled: [{required: true, message: "是否启用为必填项", trigger: "blur"}],
  sort: [{required: true, message: "排序为必填项", trigger: "blur"}],
});
const getFormRef = () => {
  return formRef.value;
}

onMounted(() => {
  deptApi.lists.get({enabled: true}).then(response => {
    deptList.value = tree.buildTree(response.data.list);
  })
  menuApi.lists.get({enabled: true}).then(response => {
    menuList.value = tree.buildTree(response.data.list);
  })
});

defineExpose({getFormRef});
</script>
